import { AuthorityInfoDTO, RoleInfo } from './../../../../shared/service-proxy/service-proxies';
import { Component, OnInit, Injector } from '@angular/core';
import { AppComponentBase } from '@shared/common/app-component-base';
import { ServiceProxy } from '@shared/service-proxy/service-proxies';
import { finalize } from 'rxjs/operators';
declare var $: any;

@Component({
  selector: 'app-change-permission-modal',
  templateUrl: './change-permission-modal.component.html',
  styleUrls: ['./change-permission-modal.component.less']
})
export class ChangePermissionModalComponent extends AppComponentBase implements OnInit {

  treeElementId = 'treePermissionsForRoles';

  isVisible: boolean = false;
  isOkLoading: boolean = false;

  roleId: string;
  constructor(
    injector: Injector,
    private _service: ServiceProxy) {
    super(injector);
  }

  ngOnInit() {
  }



  show(roleIdToSet: string): void {
    if (!roleIdToSet) {
      return;
    }

    this.roleId = roleIdToSet;

    function zTreeOnClick(event, treeId, treeNode) {
      if (!treeNode) { return; }
      const treeObj = $.fn.zTree.getZTreeObj(treeId);
      treeObj.expandNode(treeNode);
    }

    function zTreeOnRightClick(event, treeId, treeNode) {
      if (!treeNode) { return; }
      const treeObj = $.fn.zTree.getZTreeObj(treeId);
      treeObj.checkNode(treeNode, !treeNode.checked, true);
    }

    const setting = {
      view: {
        showIcon: false
      },
      check: {
        enable: true,
        chkboxType: { 'Y': 'ps', 'N': 'ps' }
      },
      data: {
        simpleData: {
          enable: true
        }
      },
      callback: {
        onClick: zTreeOnClick,
        onRightClick: zTreeOnRightClick
      }
    };

    const zNodes = [];
    this._service.getRoleUsingGET(roleIdToSet).subscribe(permisssionNames => {
      this._service.getAllAuthorityInfosUsingGET().subscribe(result => {
        result.result.fieldList.forEach(p => {
          zNodes.push({ id: p.code, pId: p.parentCode, permissionId: p.id, name: p.name, open: p.isOpen, checked: permisssionNames.result.authorityInfos.filter(m => m.id == p.id).length });
        });
        $.fn.zTree.init($('#' + this.treeElementId), setting, zNodes);
        this.isVisible = true;
      });
    });
  }



  handleOk(): void {
    this.isOkLoading = true;
    const treeObj = $.fn.zTree.getZTreeObj(this.treeElementId);
    const nodes: Array<any> = treeObj.getCheckedNodes(true);
    const permissionNames = [];

    nodes.forEach(element => {
      permissionNames.push(element.permissionId);
    });
    this._service.authorizeUsingPUT(this.roleId, permissionNames)
      .pipe(finalize(() => {
        this.isOkLoading = false;
      }))
      .subscribe(result => {
        if (result.success) {
          this.message.success(this.l("save_success"));
          this.handleCancel();
        }
        else {
          this.message.info(this.l(result.error.message));
        }
      }, error => {
        this.isOkLoading = false;
        this.message.error(this.l(error.message));
      });
  }

  handleCancel(): void {
    this.isVisible = false;
  }

}